package work.person;

import work.config.Constant;
import work.person.helper.LoggerHelper;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameIdcardReaname {


    private static File sourceFile = Constant.src;
    static String sBase = Constant.src.getAbsolutePath() + "\\";

    private static Pattern niP = Pattern.compile("^[^\\u4e00-\\u9fa5]*([\\u4e00-\\u9fa5][\\u4e00-\\u9fa5 ]{0,6}[\\u4e00-\\u9fa5]([0-9]{0,2}(?![0-9]))?)[^0-9]*([0-9X]{14,18})([^0-9]+.*)?$");

    private static Map<String, String> reaname;

    public static Map<String, String> map() {
        reaname = new HashMap<>();
        deepF();
        return reaname;
    }


    private static void deepF() {
        deep(sourceFile);
    }


    //如何确定是否应该进行深度处理
    private static void deep(File file) {

        if (isAllFileDir(file)) {
            NameIdCard nameIdCard = findNameIdCard(file);

            //进行一次搜索
            if (nameIdCard != null) {
                Dept dept = findDept(nameIdCard.file.getParentFile());
                if (dept == null) {
                    LoggerHelper.log("找不到部门对应的文件夹层级", file.getAbsolutePath().replace(sBase, ""));
                    return;
                }

                //目标文件名称
                String tName = nameIdCard.name + "   " + nameIdCard.idCard.replace('x', 'X');//将小写的文件转成大写

                File toFile = new File(dept.file, tName);
                reaname.put(file.getAbsolutePath().replace(sBase, ""), toFile.getAbsolutePath().replace(sBase, ""));
            } else {
                LoggerHelper.log("找不到存在姓名加身份证结构的文件夹层级", file.getAbsolutePath().replace(sBase, ""));
            }

        } else {

            //递归所有的子文件
            File[] files = file.listFiles();
            if (files != null && files.length > 0) {
                for (File f : files) {
                    if (f.isDirectory()) {
                        deep(f);
                    }
                }
            }

        }

    }


    //判断路径下是否都是文件
    private static boolean isAllFileDir(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null || files.length == 0) {
                return false;
            }
            for (File f : files) {
                if (!f.isFile()) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }


    //将递归放在这里
    private static NameIdCard findNameIdCard(File file) {

        File f = file;

        do {

            String name = f.getName();
            Matcher matcher = niP.matcher(name);

            //如果能够找到对应的文件就将其返回
            if (matcher.find()) {
                NameIdCard nameIdCard = new NameIdCard();
                nameIdCard.name = matcher.group(1);
                nameIdCard.idCard = matcher.group(3);
                nameIdCard.file = f;
                return nameIdCard;
            }

            //转至父节点
            f = f.getParentFile();
        } while (f != null);

        return null;
    }


    //寻找部门
    private static Dept findDept(File file) {

        File f = file;

        do {

            String name = f.getName();

            String nameRegex = "^[^\\u4e00-\\u9fa5]*([\\u4e00-\\u9fa5][\\u4e00-\\u9fa5 ]{0,6}[\\u4e00-\\u9fa5]([0-9]{0,2}(?![0-9]))?)[^0-9]*([0-9X]{14,18})([^0-9]+.*)?$";
            if (!name.matches(nameRegex)) {

                Dept dept = new Dept();

                dept.name = name;

                dept.file = f;

                return dept;

            }

            f = f.getParentFile();
        } while (f != null);

        return null;

    }


    //部门文件夹
    private static class Dept {

        private String name;

        private File file;

    }


    private static class NameIdCard {
        String name;
        String idCard;
        File file;
    }


}
